/*
 * @(#)CookieUtil.java 2007-8-29 Copyright (c) 2007. All rights reserved.
 * $Header:
 * /jsvc1/twmmc/src/java/web/com/truetel/svc/twmmc/ui/util/cookie/CookieUtil.java,v
 * 1.3 2007/09/03 06:25:12 hjsu Exp $
 */

package com.topfinance.frame.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class CookieUtil {

    private static Log log = LogFactory.getLog(CookieUtil.class);
    public static void addCookie(HttpServletResponse resp, String cookieName,
            String value) {
        if (log.isDebugEnabled()) {
            log.debug("addCookie() - " + cookieName + "=" + value);
        }
        Cookie cookie = new Cookie(cookieName, value);
        resp.addCookie(cookie);
    }

    public static void addCookie(HttpServletResponse resp, String cookieName,
            String value, int maxAge) {
        if (log.isDebugEnabled()) {
            log.debug("addCookie() - name=" + cookieName + ", value=" + value
                    + ",maxAge=" + maxAge);
        }
        Cookie cookie = new Cookie(cookieName, value);
        cookie.setMaxAge(maxAge);
        resp.addCookie(cookie);
    }

    public static void addLongLivedCookie(HttpServletResponse resp,
            String cookieName, String value){
        addLongLivedCookie(resp, cookieName, value, null);
    }

    public static void addLongLivedCookie(HttpServletResponse resp,
            String cookieName, String value, String path) {
        if (log.isDebugEnabled()) {
            log.debug("addLongLivedCookie() - " + cookieName + "=" + value);
        }
        Cookie cookie = new LongLivedCookie(cookieName, value);
        if (StringUtils.isNotBlank(path)) {
            if (log.isDebugEnabled()) {
                log.debug("addLongLivedCookie() - cookie path=" + path);
            }
            cookie.setPath(path);
        }
        resp.addCookie(cookie);

    }

    public static void removeCookie(HttpServletResponse resp, String cookieName) {
        if (log.isDebugEnabled()) {
            log.debug("removeCookie() - " + cookieName);
        }
        Cookie cookie = new ZeroLivedCookie(cookieName, null);
        resp.addCookie(cookie);
    }
    public static String getCookieValue(HttpServletRequest req,
            String cookieName){
    	return getCookieValue(req, cookieName, null);
    }
    public static String getCookieValue(HttpServletRequest req,
            String cookieName, String defalutValue){
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if (cookie.getPath() != null
                        && !cookie.getPath().startsWith(req.getContextPath())) {
                    if (log.isDebugEnabled()) {
                        log.debug("getCookieValue() - Cookie path is "
                                + cookie.getPath() + ". Skip cookie "
                                + cookie.getName());
                    }
                    continue;
                }
                if (cookieName.equals(cookie.getName())) {
                    String value = cookie.getValue();
                    if (log.isDebugEnabled()) {
                        log.debug("getCookieValue() - " + cookieName + "="
                                + value);
                    }
                    return value;
                }
            }
        }
        return defalutValue;
    }
}